<template>
  <div v-loading="allLoading" :class="{'mobile-todo-notice':showMobilePage}" class="detail">
    <Title :title="'部门成员待办事项汇总报告'"/>
    <el-row style="transform: translateX(8px)" class="mobile-todo-row">
      <el-col :span="4">
        <p><img :src="me" alt="发送人"><span class="grey-title">发送人：</span><span class="font-weight">共创平台</span></p>
      </el-col>
      <el-col :span="18">
        <p><img :src="publicTime" alt="发送时间"><span class="grey-title">发送时间：</span>
          <span class="font-weight">{{ detailData.time / 1000 | standardFormat('YYYY/MM/DD HH:mm:ss') }}</span>
        </p>
      </el-col>
    </el-row>
    <el-row>
      <p class="detail-tip">
        <span>亲爱的{{ detailData.department | formatTag }}小伙伴们：</span>
        <span style="color: #141C3D">以下是本部门成员的待办项处理情况，若有延期任务，请及时告知处理，提高工作效率</span>
      </p>
    </el-row>
    <div class="search-row">
      <el-input
        v-model.trim="params.name"
        clearable
        maxlength="100"
        size="small"
        placeholder="请输入姓名"
        class="input-width-select input-width-select-bg"
        @change="filterName"
        @clear="queryData"
        @keyup.enter.native="filterName">
        <div slot="append" class="filter-input-search" @click="filterName">
          <i class="el-icon-search" />
        </div>
      </el-input>
      <p class="reset" @click="reset">重置</p>
      <template v-if="!showMobilePage">
        <el-checkbox v-model="params.seeMy" :true-label="1" :false-label="0" @change="filterMy">只看我</el-checkbox>
        <el-checkbox v-model="params.seeValid" :true-label="1" :false-label="0" @change="queryData">只看待办人员</el-checkbox>
      </template>
    </div>
    <div v-if="showMobilePage" class="mb10">
      <el-checkbox v-model="params.seeMy" :true-label="1" :false-label="0" @change="filterMy">只看我</el-checkbox>
      <el-checkbox v-model="params.seeValid" :true-label="1" :false-label="0" @change="queryData">只看待办人员</el-checkbox>
    </div>
    <el-table
      v-loading="contractLoading"
      v-if="showContract"
      :data="contractData"
      :border="true"
      :span-method="objectSpanMethod"
      class="fb-table" >
      <el-table-column prop="type" label="任务类型" width="180" />
      <el-table-column prop="name" label="姓名" min-width="100" />
      <el-table-column
        :sort-by="'pending'"
        label="待处理"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.pending > 0 }">{{ scope.row.pending }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :sort-by="'delay'"
        label="已延期"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.delay > 0 }">{{ scope.row.delay }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :sort-by="'notRecycle'"
        label="未回收"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.notRecycle > 0 }">{{ scope.row.notRecycle }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :sort-by="'notRecycleDelay'"
        label="延期未回收"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.notRecycleDelay > 0 }">{{ scope.row.notRecycleDelay }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-table
      v-loading="fundLoading"
      v-if="showFund"
      :data="fundData"
      :border="true"
      :span-method="objectSpanMethod"
      class="fb-table">
      <el-table-column prop="type" label="任务类型" width="180" />
      <el-table-column prop="name" label="姓名" min-width="100"/>
      <el-table-column
        :sort-by="'pending'"
        label="待处理"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.pending > 0 }">{{ scope.row.pending }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :sort-by="'delay'"
        label="已延期"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.delay > 0 }">{{ scope.row.delay }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-table
      v-loading="channelLoading"
      v-if="showChannel"
      :data="channelData"
      :border="true"
      :span-method="objectSpanMethod"
      class="fb-table">
      <el-table-column prop="type" label="任务类型" width="180" />
      <el-table-column prop="name" label="姓名" min-width="100"/>
      <el-table-column
        :sort-by="'beReleased'"
        label="待发布"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.beReleased > 0 }">{{ scope.row.beReleased }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :sort-by="'beApprove'"
        label="待审批"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.beApprove > 0 }">{{ scope.row.beApprove }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-table
      v-loading="projectLoading"
      v-if="showProject"
      :data="projectData"
      :border="true"
      :span-method="objectSpanMethod"
      class="fb-table">
      <el-table-column prop="type" label="任务类型" width="180" />
      <el-table-column prop="name" label="姓名" min-width="100"/>
      <el-table-column
        :sort-by="'beSubmit'"
        label="待提交"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.beSubmit > 0 }">{{ scope.row.beSubmit }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :sort-by="'beApprove'"
        label="待初审"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.beApprove > 0 }">{{ scope.row.beApprove }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :sort-by="'beReapprove'"
        label="待复审"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.beReapprove > 0 }">{{ scope.row.beReapprove }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :sort-by="'beConfirm'"
        label="待确认"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.beConfirm > 0 }">{{ scope.row.beConfirm }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :sort-by="'beArchive'"
        label="待封存"
        min-width="180"
        sortable>
        <template slot-scope="scope">
          <span :class="{ 'red': scope.row.beArchive > 0 }">{{ scope.row.beArchive }}</span>
        </template>
      </el-table-column>
    </el-table>
    <fixed-pagination>
      <div class="center">
        <el-button
          class="back-btn cancel-btn-style"
          type="info"
          size="small"
          @click="handleBack"
        >返回</el-button>
      </div>
    </fixed-pagination>
  </div>
</template>

<script>
import Title from '@/components/Title';
import { getDetailList, getContractList, getFundList, getProjectList, getChannelList } from '@/api/todo';
import { standardFormat } from '@/filters/date';
import { formatTag } from '@/filters/dept';
import me from '@/assets/todo/me.png';
import publicTime from '@/assets/todo/publicTime.png';
import mobileTable from '@/mixins/mobileTable';

export default {
  components: { Title },
  filters: { standardFormat, formatTag },
  mixins: [mobileTable],
  data() {
    return {
      me,
      publicTime,
      params: {
        name: '',
        seeMy: 0,
        seeValid: 0
      },
      todoList: [],
      showContract: false,
      contractData: [],
      contractArr: [],
      contractPos: 0,
      contractLoading: false,
      showFund: false,
      fundData: [],
      fundArr: [],
      fundPos: 0,
      fundLoading: false,
      showProject: false,
      projectData: [],
      projectArr: [],
      projectPos: 0,
      projectLoading: false,
      showChannel: false,
      channelData: [],
      channelArr: [],
      channelPos: 0,
      channelLoading: false,
      detailData: {},
      spanArr: [],
      pos: 0
    };
  },
  computed: {
    allLoading() {
      return this.contractLoading && this.fundLoading && this.channelLoading && this.projectLoading;
    }
  },
  created() {
    this.getAllData();
  },
  methods: {
    getAllData() {
      this.params.id = this.$route.params.id;
      this.showContract = false;
      this.showFund = false;
      this.showChannel = false;
      this.showProject = false;
      getDetailList({ id: this.$route.params.id }).then(res => {
        this.detailData = res.data;
      }).catch(err => {
        console.log(err);
      });
      if (this.$route.query.todoList !== null && this.$route.query.todoList instanceof Array) {
        this.todoList = this.$route.query.todoList[0].split(',');
      } else if (this.$route.query.todoList !== null && typeof this.$route.query.todoList === 'string') {
        this.todoList = this.$route.query.todoList.split(',');
      }
      this.todoList.forEach(item => {
        switch (item) {
          case '0':
            this.showContract = true;
            this.getData(getContractList, 'contractLoading', 'contractData', '合同审批', 'contractArr', 'contractPos');
            break;
          case '1':
            this.showFund = true;
            this.getData(getFundList, 'fundLoading', 'fundData', '请款审批', 'fundArr', 'fundPos');
            break;
          case '2':
            this.showChannel = true;
            this.getData(getChannelList, 'channelLoading', 'channelData', '渠道订单', 'channelArr', 'channelPos');
            break;
          case '3':
            this.showProject = true;
            this.getData(getProjectList, 'projectLoading', 'projectData', '项目订单', 'projectArr', 'projectPos');
            break;
        }
      });
    },
    filterName() {
      this.params.seeMy = 0;
      this.queryData();
    },
    filterMy() {
      this.params.name = '';
      this.queryData();
    },
    queryData() {
      this.todoList.forEach(item => {
        switch (item) {
          case '0':
            this.getData(getContractList, 'contractLoading', 'contractData', '合同审批', 'contractArr', 'contractPos');
            break;
          case '1':
            this.getData(getFundList, 'fundLoading', 'fundData', '请款审批', 'fundArr', 'fundPos');
            break;
          case '2':
            this.getData(getChannelList, 'channelLoading', 'channelData', '渠道订单', 'channelArr', 'channelPos');
            break;
          case '3':
            this.getData(getProjectList, 'projectLoading', 'projectData', '项目订单', 'projectArr', 'projectPos');
            break;
        }
      });
    },
    reset() {
      this.params = { id: this.$route.params.id, seeMy: 0, seeValid: 0 };
      this.queryData();
    },
    getSpanArr(data, arr, pos) {
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          arr.push(1);
          // pos = 0;
        } else {
          // 判断当前元素与上一个元素是否相同
          // if (data[i].id === data[i - 1].id) {
          arr[pos] += 1;
          arr.push(0);
          // } else {
          //   arr.push(1);
          //   pos = i;
          // }
        }
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this[row.table][rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          // [0,0] 表示这一行不显示， [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        };
      }
    },
    async getData(name, loading, data, type, spanArr, spanPos) {
      this[loading] = true;
      try {
        const res = await name(this.params);
        // this[data] = res.data.items;
        if (res.data.length === 0) {
          // res.data[0].type = type;
          res.data.push({ type: type });
        }
        this[data] = res.data;
        this[loading] = false;
        this[data].forEach(item => {
          item.type = type;
          item.table = spanArr;
        });
        this.getSpanArr(this[data], this[spanArr], this[spanPos]);
      } catch (err) {
        this[loading] = false;
        console.log(err);
      }
    },
    handleBack() {
      if (this.$route.query.fromDingDing) {
        this.$router.push({
          name: 'todo',
          query: { activeId: 3, fromDingDing: this.$route.query.fromDingDing }
        });
      } else {
        this.$router.push({
          name: 'todo',
          query: { activeId: 3 }
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.detail {
  &::v-deep .el-row {
    margin-top: 16px;
  }
  P {
    display: flex;
    align-items: center;
    margin: 0;
    font-size: 14px;
    img {
      width: 15px;
      height: 15px;
      margin-right: 4px;
    }
    .grey-title {
      color: #999;
    }
    .font-weight {
      color: #333;
      font-weight: bold;
    }
  }
}
.search-row {
  display: flex;
  align-items: center;
  margin: 12px 0;
  &::v-deep {
    .input-width-select-bg {
      &> .el-input__inner {
        background-color: #fff;
      }
    }
    .el-input-group__append {
      border-left: 1px solid #406eff;
      border-color: #406eff;
      padding: 0;
      margin: 0;
      width: 54px;
      overflow: hidden;
      background-color: #406eff;
      .filter-input-search {
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 0px;
        border: none;
        padding: 0;
        width: 54px;
        height: 30px;
        // background: #406eff;
        cursor: pointer;
        .el-icon-search {
          font-size: 16px;
          color: #fff;
        }
      }
    }
  }
  .el-input {
    width: 240px;
  }
  .reset {
    margin: 0 24px 0 16px;
    color: #666;
    cursor: pointer;
  }
  &::v-deep .el-checkbox{
    display: flex;
    align-items: center;
  }
}
.detail-tip {
  margin: 16px 0 12px 0;
  padding: 8px;
  background-color: #f9f9f9;
  span {
    color: #666;
  }
}
.el-table{
  margin-bottom: 16px;
  .red {
    color: #FF4E5F;
  }
}
.mobile-todo-notice {
  .mobile-todo-row {
    display: flex;
    flex-wrap: wrap;
    ::v-deep .el-col {
      width: 100% !important;
    }
  }
  .detail-tip {
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>
